<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/5
 * Time: 8:25
 */

namespace Admin\Controller;

use Think\Page;

class OrderController extends AdminController
{
    public function detail()
    {
        $id = I('id','');
        $order = M('Order')->where("id=" . $id)->find();

        $items = M('Order_detail')->where('oid='.$id)->select();

        $book = M('Order_type')->where('id='.$order['bid'])->find();

        $roots = M('Order_root')->where(array('bid'=>$order['bid'],'level'=>1))->select();

        foreach ($roots as $i => $root){
            $tmp = $this->getChildren($root['id']);
            if (sizeof($tmp)>0) {
                $roots[$i]['child'] = $tmp;
                foreach ($roots[$i]['child'] as $j => $ts){
                    $tp = $this->getChildren($ts['id']);
                    if (sizeof($tp)>0) {
                        $roots[$i]['child'][$j]['child'] = $this->check($tp,$items);
                    }
                }
            }
        }

        $this->assign('book',$book);
        $this->assign('roots',$roots);
        $this->assign('order', $order);
        $this->display();
    }

    function check($children,$result){
        foreach ($children as $i => $v){
            $res = $this->checked($v['id'],$result);
            if ($res){
                $children[$i]['checked'] = 1;
                $children[$i]['value'] = $res['value'];
            }
        }
        return $children;
    }

    function checked($vid,$result){
        foreach ($result as $i => $v){
            if ($vid==$v['aid'])
                return $v;
        }
        return false;
    }

    function getChildren($pid){
        $children = M('Order_root')->where('pid='.$pid)->select();
        return $children;
    }

    public function finish()
    {
        $p = I('p', '1');
        $Order = M('Order');
        $page_size = 20;
        $count = $Order->where('mark=1')->count();
        $list = $Order->page($p . ',' . $page_size)->where('mark=1')->select();
        $page = new Page($count, $page_size);
        $books = M('Order_type')->select();
        foreach ($list as $i => $v){
            foreach ($books as $j => $t){
                if ($v['bid']==$t['id']){
                    $list[$i]['book'] = $t['name'];
                }
            }
        }
        $this->assign('list', $list);
        $this->assign('page', $page->show());
        $this->display();
    }

    public function unfinish()
    {
        $p = I('p', '1');
        $Order = M('Order');
        $page_size = 20;
        $count = $Order->where('mark=0')->count();
        $list = $Order->page($p . ',' . $page_size)->where('mark=0')->select();
        $page = new Page($count, $page_size);
        $books = M('Order_type')->select();
        foreach ($list as $i => $v){
            foreach ($books as $j => $t){
                if ($v['bid']==$t['id']){
                    $list[$i]['book'] = $t['name'];
                }
            }
        }
        $this->assign('list', $list);
        $this->assign('page', $page->show());
        $this->display();
    }

    public function delete()
    {
        $Order = M('Order');
        $id = I('id');
        $res = $Order->where("id=" . $id)->delete();
        if ($res) {
            $this->success('删除成功!');
        } else {
            $this->error('删除成功!');
        }
    }

    public function mark()
    {
        $Order = M('Order');
        $id = I('id');
        $data = $Order->where("id=" . $id)->data('mark=1')->save();
        if ($data) {
            $this->success("修改成功!");
        } else {
            $this->error("修改失败!");
        }
    }

    public function manager(){
        $orders = M('Order_type')->select();
        $this->assign('orders',$orders);
        $this->display();
    }

    public function newOrder(){
        $this->display();
    }

    public function saveOrder(){
        $data['name'] = I('name','');
        $data['remark'] = I('remark','');
        $res = M('Order_type')->data($data)->add();
        if ($res){
            $this->success('添加成功!');
        }else {
            $this->error('添加失败!');
        }
    }

    public function root_delete(){
        $id = I('id','');
        if ($id!=''){
            M('Order_root')->where('bid='.$id)->delete();
            $res = M('Order_type')->where('id='.$id)->delete();
            if ($res){
                $this->success('删除成功!');
            }else {
                $this->error('删除失败!');
            }
        }

    }

    public function roots(){
        $id = I('id','');
        $roots = M('Order_root')->where(array('bid'=>$id,'level'=>1))->select();
        $order = M('Order_type')->where('id='.$id)->find();
        foreach ($roots as $i => $root){
            $tmp = $this->getChildren($root['id']);
            if (sizeof($tmp)>0) {
                $roots[$i]['child'] = $tmp;
                foreach ($roots[$i]['child'] as $j => $ts){
                    $tp = $this->getChildren($ts['id']);
                    if (sizeof($tp)>0) {
                        $roots[$i]['child'][$j]['child'] = $tp;
                    }
                }
            }
        }
//        dump($roots);
        $this->assign('bid',$id);
        $this->assign('order',$order);
        $this->assign('roots',$roots);
        $this->display();
    }

    public function add_root(){
        $bid = I('bid','');
        $pid = I('pid','0');
        $parent = M('Order_root')->where('id='.$pid)->find();
        $order = M('Order_type')->where('id='.$bid)->find();
        $this->assign('order',$order);
        $this->assign('parent',$parent);
        $this->display();
    }

    public function edit_root(){
        $id = I('id','');
        $self = M('Order_root')->where('id='.$id)->find();
        $parent = M('Order_root')->where('id='.$self['pid'])->find();
        $this->assign('parent',$parent);
        $this->assign('self',$self);
        $this->display();
    }

    public function update_root(){
        $data['name'] = I('name','');
        $data['level'] = I('level','');
        $data['pid'] = I('pid','');
        $data['type'] = I('type','');
        $data['remark'] = I('remark','');
        $data['bid'] = I('bid','');
        $id = I('id','');

        if ($id){
            $data['id'] = $id;
            $res = M('Order_root')->data($data)->save();
            if ($res) {
                $this->success('保存成功!', U('roots', array('id' => $data['bid'])));
            } else {
                $this->error('保存失败!', U('roots', array('id' => $data['bid'])));
            }
        }else {
            $res = M('Order_root')->data($data)->add();
            if ($res) {
                $this->success('添加成功!', U('roots', array('id' => $data['bid'])));
            } else {
                $this->error('添加失败!', U('roots', array('id' => $data['bid'])));
            }
        }
    }

    public function delete_root(){
        $id = I('id','0');
        $bid = I('bid','');

        $self = M('Order_root')->where('id=' . $id)->delete();

        if ($self) {
            $ids = M('Order_root')->where('pid=' . $id)->field('id')->select();
            M('Order_root')->where('pid=' . $id)->delete();
            if (sizeof($ids)) {
                foreach ($ids as $i => $d) {
                    M('Order_root')->where('pid=' . $d['id'])->delete();
                }
            }
            $this->success('删除成功!',U('roots',array('id'=>$bid)));
        }else{
            $this->error('删除失败!',U('roots',array('id'=>$bid)));
        }
    }

    public function show(){
        $id = I('id','');
        $roots = M('Order_root')->where(array('bid'=>$id,'level'=>1))->select();
        $order = M('Order_type')->where('id='.$id)->find();
        foreach ($roots as $i => $root){
            $tmp = $this->getChildren($root['id']);
            if (sizeof($tmp)>0) {
                $roots[$i]['child'] = $tmp;
                foreach ($roots[$i]['child'] as $j => $ts){
                    $tp = $this->getChildren($ts['id']);
                    if (sizeof($tp)>0) {
                        $roots[$i]['child'][$j]['child'] = $tp;
                    }
                }
            }
        }
//        dump($roots);
        $this->assign('bid',$id);
        $this->assign('order',$order);
        $this->assign('roots',$roots);
        $this->display();
    }
}